package com.doudou.collection.adapter.controller;



import com.doudou.collection.domain.service.BlogService;
import com.doudou.collection.infrastructure.common.security.CurrentPrincipal;
import com.doudou.collection.infrastructure.common.web.ResultVO;
import com.doudou.collection.orm.dto.BlogDTO;
import com.doudou.collection.orm.vo.BlogCategoriesVO;
import com.doudou.collection.orm.vo.BlogDetailVO;
import com.doudou.collection.orm.vo.BlogSimpleVO;
import com.doudou.collection.orm.vo.ContentIndexVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName BlogController<br>
 * @Description 博客文章管理模块<br>
 * @Author 李浩文<br>
 * @Date 2023/6/2 16:02<br>
 * @Version 1.0<br>
 */
@Api(tags = "02.博客文章管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/blogs/")
@Validated
public class BlogController {

    @Autowired
    private BlogService blogService;

    @ApiOperation("发布文章功能")
    @ApiOperationSupport(order = 100)
    @PostMapping("publish")
    public ResultVO insert(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                           @ApiIgnore HttpServletRequest request,
                           @Validated BlogDTO blogDTO) {
        log.debug("正在处理发布文章请求");
        log.debug("用户发布的文章信息 {}", blogDTO);
        String articleLocation = request.getRemoteAddr();
        blogService.insert(currentPrincipal,articleLocation,blogDTO);
        return ResultVO.ok();
    }


    @ApiOperation("删除文章功能")
    @ApiOperationSupport(order = 200)
    @DeleteMapping("/{id}/delete")
    public ResultVO delete(@PathVariable Long id){
        log.debug("正在处理根据id删除文章请求,id为{}",id);

        blogService.delete(id);
        return ResultVO.ok();
    }


    @ApiOperation("根据id显示文章详情功能")
    @ApiOperationSupport(order = 400)
    @GetMapping("{id}/detail")
    public ResultVO detail(@PathVariable Long id) {
        log.debug("正在处理根据id查看文章具体详情请求,id为{}",id);
        BlogDetailVO blogDetailVO = blogService.detailById(id);

        return ResultVO.ok(blogDetailVO);
    }



    @ApiOperation("获取所有文章类别")
    @ApiOperationSupport(order = 410)
    @GetMapping("/allCategories")
    public ResultVO allCategories() {
        log.debug("正在处理获取文章所有类别请求");
        List<BlogCategoriesVO> blogCategories = blogService.selectAllCategories();

        return ResultVO.ok(blogCategories);
    }


    @ApiOperation("根据分类id获取分类下的所有文章功能")
    @ApiOperationSupport(order = 420)
    @GetMapping("/{categoriesId}/index")
    public ResultVO queryAllArticle(@PathVariable Integer categoriesId) {
        log.debug("正在处理根据分类id获取文章请求,分类id为 {}", categoriesId);
        List<ContentIndexVO> contentIndexVO = blogService.queryAllArticle(categoriesId);

        return ResultVO.ok(contentIndexVO);

    }
    @ApiOperation("显示推荐的所有文章功能")
    @ApiOperationSupport(order = 430)
    @GetMapping("/0/index")
    public ResultVO recommendedBlog(){
        log.debug("正在处理获取推荐文章请求");
        List<ContentIndexVO> contentIndexVO = blogService.recommendedBlog();

        return ResultVO.ok(contentIndexVO);
    }




    @ApiOperation("在文章详情页获取该作者的其他文章")
    @ApiOperationSupport(order = 440)
    @GetMapping("/{userId}/others")
    public ResultVO selectOthersByUserId(@PathVariable Long userId){
        log.debug("正在处理在文章详情页获取该作者的其他文章请求");
        List<BlogSimpleVO> blogSimpleVO = blogService.selectOthersByUserId(userId);

        return ResultVO.ok(blogSimpleVO);

    }


    @ApiOperation("获取热门文章功能")
    @ApiOperationSupport(order = 450)
    @GetMapping("/hot")
    public ResultVO selectHot(){
        log.debug("正在处理获取热门文章请求");
        List<BlogSimpleVO> blogSimpleVO = blogService.selectHot();

        return ResultVO.ok(blogSimpleVO);

    }



    @ApiOperation("根据关键字搜索文章功能")
    @ApiOperationSupport(order = 460)
    @GetMapping("/{wd}/search")
    public ResultVO search(@PathVariable String wd) {
        log.debug("正在处理根据关键字搜索文章请求，关键字为：{}",wd);

        return ResultVO.ok(blogService.search(wd));
    }


    @ApiOperation("个人主页我的文章展示")
    @ApiOperationSupport(order = 470)
    @GetMapping("/{id}/selectselfblogs")
    public ResultVO selectSelfBlogs(@PathVariable Long id) {
        log.debug("正在处理个人主页我的文章请求, {}", id);
        List<ContentIndexVO> contentIndexVO = blogService.selectSelfBlogsById(id);

        return ResultVO.ok(contentIndexVO);
    }


    @ApiOperation("个人主页我赞的文章展示")
    @ApiOperationSupport(order = 471)
    @GetMapping("/{id}/likeblogsall")
    public ResultVO selectLikeBlogsAll(@PathVariable Long id) {
        log.debug("正在处理个人主页点赞的所有文章请求, {}", id);
        List<ContentIndexVO> contentIndexVO = blogService.selectLikeBlogsAllById(id);

        return ResultVO.ok(contentIndexVO);
    }


    @ApiOperation("个人主页我收藏的文章展示")
    @ApiOperationSupport(order = 472)
    @GetMapping("{id}/collectblogs")
    public  ResultVO selectCollectBlogsAll(@PathVariable Long id) {
        log.debug("正在处理个人主页收藏的所有文章请求, {}", id);
        List<ContentIndexVO> contentIndexVO = blogService.selectCollectBlogsAllById(id);

        return ResultVO.ok(contentIndexVO);
    }

    @ApiOperation("文章被点赞数量")
    @ApiOperationSupport(order = 473)
    @PostMapping("{blogId}/bloglikedcount")
    public ResultVO updateBlogLikedCount(@PathVariable Long blogId){
        log.debug("正在通过文章id处理文章被点赞数量, {}", blogId);
        blogService.updateBlogLikedFromUserLiked(blogId);
        return ResultVO.ok();
    }


}













